import TableLyout from '@/components/commonPublic/tableLyoutLong.vue'
import tree from "@/components/materialQualityPage/machineNetworkManage/tree"; //左侧机构树
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import lineEcharts from "@/components/materialQualityPage/machineNetworkManage/analysisDataManage/cementQuality/lineEcharts.vue"; //折线图


import formJs from "./regexDetail.js"; //新增弹框配置
import {
    getTestPasteTrendsList, //列表页接口
    getBaseDataitemDetailByItemCode, //字典
    getUserCompanyTreeList, //树状图
} from "@/api/request.js";


export default {
    name: "pasteQuality",
    data() {
        return {
            name: "pasteQuality",
            searchObj: formJs.searchObj, //搜索条件
            loading: false,
            btnObj: [], //按钮
            tableData: {},//全部数据
            // 图表

            getList: {
                MachineId: "",
                fCementtype: "",
                fCloseoperation: "",
                keyWord: "",
                modelType: "0",
                page: 1,
                pageSize: 20,
                keyType: "", // 上方查询条件试验类型
                upNum: "",//波动上限
                downNum: "",//波动下限
                taskNumber: "",//区段
                fCementType: "",//水泥品种
                fStrength: "", //上方查询条件强度等级


                fCompanyid: "", // 左侧选中组织机构id
                fAge: "",//上方查询条件龄期
                userId: "", //用户id
                startDates: "", //开始时间
                endDates: "", //结束时间
                timeDates: "",
            },


            // 折线图
            groupNmu: 0,
            groupNmu1: 0,
            lineChartData: null,//折线图数据
            echartsTitle: "",//折线图标题
            lineChartData1: null,//折线图数据--折线图2
            echartsTitle1: "",//折线图标题---折线图2
            xName: "时间",//X轴名称
            yName: "(MPa)",//Y轴名称
            // 折线图

            // 图表

            // 机构树
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            treeText: {
                companyId: null,
                userId: null,
            },
            companyTree: "", //所有数据
            treeData: [], //左侧树状图数据
            highlightBd: true, //左侧机构树停留背景色
            treelength: false, //左侧树显示隐藏
            // checked: true, //查看下级按钮
            showName: "未选择公司",
            checkData: [], //树状图默认选中id
            // 机构树

        };
    },
    components: {
        TableLyout,
        publicForm,
        tree,
        elBreadcrumb,
        lineEcharts,
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));


        this.getList = {
            MachineId: "",
            fCementtype: "",
            fCloseoperation: "",
            keyWord: "",
            modelType: "",
            page: 1,
            pageSize: 20,
            keyType: "", //上方查询条件材料种类
            upNum: "",//波动上限
            downNum: "",//波动下限
            taskNumber: "",//区段
            fCementType: "",//水泥品种
            fStrength: "", //上方查询条件强度等级

            fCompanyid: "", // 左侧选中组织机构id
            fAge: "", //上方查询条件龄期
            userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
            startDates: "", //进场开始时间
            endDates: "", //进场结束时间
            timeDates: [],
        };
        this.getSpecimenage("SpecimenAge4");//龄期
        this.btnRole(this.name)

    },
    methods: {
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            console.log(name, status);
        },
        // 左侧树结构
        getTree() {
            this.treeText = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId,
                // text: "1", // 0其他  1 砼拌合站 2水稳 3沥青
            };
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }
                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("yjSmart-machineNetworkManage_treeCheck")
                            ).fShortname;
                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                            this.checkData[0] = this.treeData[0].fCompanyid;
                        }

                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fShortname; //顶部显示机构名
            this.getList.fCompanyid = data.fCompanyid;
            this.yj_searchForm(this.getList);
        },



        // 获取列表数据
        yj_searchForm(data) {
            if (data.fAge == "龄期(天)") {
                data.fAge = "";
            }
            window.sessionStorage.setItem(
                "yjSmart-machineNetworkManage_getList",
                JSON.stringify(data)
            );
            var obj = {
                fCompanyid: data.fCompanyid, // 左侧选中组织机构id
                fAge: data.fAge, //上方查询条件龄期
                userId: this.userInfo.userId, //用户id
                startDates: data.startDates, //开始时间
                endDates: data.endDates, //结束时间
            }

            this.echartsTitle = obj.startDates + "至" + obj.endDates + "净浆质量波动图(抗压)";
            this.echartsTitle1 = obj.startDates + "至" + obj.endDates + "净浆质量波动图(抗折)";
            this.groupNmu = 0;
            this.groupNmu1 = 0;
            this.loading = true;
            getTestPasteTrendsList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        var that = this;
                        that.loading = false;

                        that.tableData = res.data;

                        // 抗压
                        that.lineChartData = {
                            xitems: [], //试验日期  X
                            yitems: [],//最大力强度  Y
                            fSamplenostr: [],//试件编号
                            fProjectlocation: [],//工程部位

                        }
                        if (res.data.listky.length != 0) {
                            that.groupNmu = res.data.listky.length;
                            res.data.listky.map(item => {
                                that.lineChartData.xitems.push(item.fTestdates);
                                that.lineChartData.yitems.push(item.fStrengthmeasurement);
                                that.lineChartData.fSamplenostr.push(item.fSamplenostr);
                                that.lineChartData.fProjectlocation.push(item.fProjectlocation);

                            })
                        }
                        // 抗折
                        that.lineChartData1 = {
                            xitems: [], //试验日期  X
                            yitems: [],//最大力强度  Y
                            fSamplenostr: [],//试件编号
                            fProjectlocation: [],//工程部位
                        }
                        if (res.data.listkz.length != 0) {
                            that.groupNmu1 = res.data.listkz.length;
                            res.data.listkz.map(item => {
                                that.lineChartData1.xitems.push(item.fTestdates);
                                that.lineChartData1.yitems.push(item.fStrengthmeasurement);
                                that.lineChartData1.fSamplenostr.push(item.fSamplenostr);
                                that.lineChartData1.fProjectlocation.push(item.fProjectlocation);
                            })
                        }


                        // 折线区域数据

                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    console.log("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },

        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0]; // 开始时间
                this.getList.endDates = e[1]; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]]
            this.yj_searchForm(this.getList);

        },

        getDate() {
            if (JSON.parse(sessionStorage.getItem("yjSmart-machineNetworkManage_treeCheck"))) {
                var id = JSON.parse(sessionStorage.getItem("yjSmart-machineNetworkManage_treeCheck"));
                this.checkData = [id.fCompanyid];
            }
            if (JSON.parse(sessionStorage.getItem("yjSmart-machineNetworkManage_getList"))) {
                var getList = JSON.parse(sessionStorage.getItem("yjSmart-machineNetworkManage_getList"));
                this.getList.startDates = getList.startDates; // 开始时间
                this.getList.endDates = getList.endDates;  // 截至时间
                this.getList.timeDates = getList.timeDates;
            } else {
                this.getList.startDates = this.common.getDay(-6); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [this.getList.startDates, this.getList.endDates];
            }
            this.getTree();
        },

        // 字典项
        //试件龄期
        getSpecimenage(data) {
            getBaseDataitemDetailByItemCode({ fItemcode: data })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.searchObj[1].options = [{
                            value: "龄期(天)",
                            label: "龄期(天)"
                        }];
                        res.data.baseDataitemdetailList.map((item) => {
                            this.searchObj[1].options.push({
                                value: item.fItemname,
                                label: item.fItemvalue,
                            });
                        });
                        this.getList.fAge = this.searchObj[1].options[0].label;
                        this.getDate();

                    } else {
                        this.$message.error("试件龄期", res.msg);
                    }
                })
                .catch(() => {
                    console.log("字典--试件龄期接口报错");
                });
        },
        // 字典项
    },
};